package com.fangrui.mapper;

import com.fangrui.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EmpMapper {
    @Delete("delete from emp02 where id = #{id}")
    public int delete(Integer id);

    @Options(keyProperty = "id", useGeneratedKeys = true)//会自动将生成的主键赋值给emp的id属性
    @Insert("insert into emp02 (username,name,gender,image,job,entrydate,dept_id,create_time,update_time) values (#{username},#{name},#{gender},#{image},#{job},#{entryDate},#{deptId},#{createTime},#{updateTime})")
    public int insert(Emp emp);

    @Update("update emp02 set username = #{username},name = #{name},gender = #{gender},image=#{image},job=#{job},entrydate=#{entryDate},dept_id=#{deptId},create_time=#{createTime},update_time=#{updateTime} where id =#{id} ")
    public int update(Emp emp);

    public int update_xml(Emp emp);

    //由于字段名与实体类属性名不一致，导致数据封装失败
    @Select("select * from emp02 where id = #{id}")
    public Emp selectById(Integer id);

    //解决方法一：取别名
    @Select("select id,username,password,name,gender,image,job,entrydate,dept_id as deptId,create_time as createTime,update_time as updateTime from emp02 where id = #{id}")
    public Emp selectById01(Integer id);

    //解决方案二：通过@Result注解，@Result注解手动映射封装
    @Results({
            @Result(column = "dept_id",property = "deptId"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from emp02 where id = #{id}")
    public Emp selectById02(Integer id);

    //解决方案三：开启mybatis驼峰自动映射开关，在properties配置文件中开启

    //条件查询
    @Select("select * from emp02 where name like concat('%',#{name},'%') and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc")
    public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);

    @Select("select * from emp02 where name like '%${name}%' and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc")
    public List<Emp> list_danger(String name, Short gender, LocalDate begin, LocalDate end);

    public List<Emp> list_xml(String name, Short gender, LocalDate begin, LocalDate end);

    public List<Emp> list_xml_field(Short gender, LocalDate begin, LocalDate end);

    public void deleteByIds(List<Integer> ids);

}
